package com.ldgame.utils;

import org.newdawn.slick.Color;

import com.ldgame.gfx.gfxelements.GfxComponent;
import com.ldgame.gfx.gfxelements.GfxDecorator;
import com.ldgame.input.Input;

public class Button extends GfxDecorator {
	private boolean enabled = true;
	private BtnAction action;
	private Color idleColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
	private Color activeColor = new Color(1.0f, 0.0f, 0.0f, 1.0f);
	
	public Button(GfxComponent component, BtnAction action) {
		super(component);
		getComponent().setColor(idleColor);
		this.action = action;
	}
	
	public void setColors(Color idleColor, Color activecolor) {
		this.idleColor = idleColor;
		this.activeColor = activecolor;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	private boolean checkMouseOver() {
		return ((float)Input.getMouseX() > getComponent().getPosition().x - 0.5f * getComponent().getWidth() &&
				(float)Input.getMouseX() < getComponent().getPosition().x + 0.5f * getComponent().getWidth() &&
				(float)Input.getMouseY() > getComponent().getPosition().y - 0.5f * getComponent().getHeight() &&
				(float)Input.getMouseY() < getComponent().getPosition().y + 0.5f * getComponent().getHeight());
	}
	
	public void listen() {
		if (checkMouseOver()) {
			getComponent().setColor(activeColor);
			if (Input.checkReleasedMouseBtn(0)) {
				action.executeAction();
			}
		} else {
			getComponent().setColor(idleColor);
		}
	}
}
